package touche.touche;

import model.GameManager;
import model.ScoresList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class SendScoreActivity extends Activity implements
		View.OnClickListener, OnEditorActionListener {
	private Button _buttonSend;
	private Button _buttonMenu;

	private EditText _editTextNickname;

	public void onClick(View v) {
		if (_buttonSend == v) {
			sendScore();
		} else if (_buttonMenu == v) {
			finish();
		}
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send_score);

		_buttonSend = (Button) findViewById(R.id.buttonSend);
		_buttonSend.setOnClickListener(this);

		_buttonMenu = (Button) findViewById(R.id.buttonMenu);
		_buttonMenu.setOnClickListener(this);

		_editTextNickname = (EditText) findViewById(R.id.editTextNickname);
		_editTextNickname.setOnEditorActionListener(this);
	}

	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if (actionId == EditorInfo.IME_ACTION_SEND) {
			sendScore();
		}

		return false;
	}

	private void sendScore() {
		if (_editTextNickname.getText().toString().equals("")) {
			Toast.makeText(getBaseContext(),
					this.getText(R.string.nickname_mandatory),
					Toast.LENGTH_SHORT).show();
			return;
		} else {
			TelephonyManager telephonyManager = (TelephonyManager) getBaseContext()
					.getSystemService(Context.TELEPHONY_SERVICE);
			ScoresList.getInstance().addScore(
					GameManager.getInstance().getScore(),
					_editTextNickname.getText().toString(),
					telephonyManager.getDeviceId(),
					telephonyManager.getDeviceSoftwareVersion());
			Toast.makeText(getBaseContext(),
					this.getText(R.string.score_added), Toast.LENGTH_SHORT)
					.show();
			finish();
		}
	}
}